﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Joyeon.Utils
{
    public static class FileUtil
    {
        private const double B = 1d;
        private const double K = 1024 * 1d;
        private const double M = 1024 * 1024 * 1d;
        private const double G = 1024 * 1024 * 1024 * 1d;
        private const String TEMPLATE = "{0:N2}";
        /// <summary>
        /// 根据值的大小输出格式化后的计算机表示值
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public static String GetFormatedSize(double size)
        {
            double result = 0;
            String format = TEMPLATE;
            if (size > G)
            {
                result = size / G;
                format += "G";
            }
            else if (size > M)
            {
                result = size / M;
                format += "M";
            }
            else if (size > K)
            {
                result = size / K;
                format += "K";
            }
            else
            {
                result = size / B;
                format += "B";
            }
            return String.Format(format, result);
        }
    }
}
